﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BusinessLogicLayer.Base;
using DALLayer.Providers;
using DataDefinition;
using Objects;

namespace BusinessLogicLayer.Manager
{
    public class VehicleModelTypeManager : ManagerBase<VehicleModelTypeDataProvider>
    {
        #region [Constructor]
         /// <summary>
        /// Initializes a new instance of the <see cref="VehicleModelTypeManager" /> class.
        /// </summary>
        public VehicleModelTypeManager() { }
        #endregion

        #region [Manager base class implementations]
        /// <summary>
        /// Pass the data provider to the class
        /// </summary>
        protected override void InitializeDataProvider()
        {
            //Pass the specific data provider to the manager class
            base.DataProvider = new VehicleModelTypeDataProvider();
        }

        #endregion
        #region [Public Methods]
        /// <summary>
        /// Update or insert a vehicle model type.
        /// </summary>
        /// <param name="vehicleModelType">vehicle model type object</param>
        /// <returns>true or false</returns>
        public bool UpdateVehicleModelType(VehicleModelType vehicleModelType)
        {
            bool success = false;
            try
            {
                //if there is an id
                if (vehicleModelType.VehicleModelTypeID > 0)
                {
                    //Update the vehicle model type
                    success = base.DataProvider.UpdateVehicleModelType(vehicleModelType.VehicleModelTypeID, vehicleModelType.VehicleModelTypeName, vehicleModelType.VehicleModelID, vehicleModelType.IsActive);
                }
                else
                {
                    //Insert an vehicle model type
                    success = base.DataProvider.InsertVehicleModelType(vehicleModelType.VehicleModelTypeName, vehicleModelType.VehicleModelID, vehicleModelType.IsActive);

                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            //returns true or false
            return success;

        }
        /// <summary>
        /// insert a vehicle model type returns added record object.
        /// </summary>
        /// <param name="vehicleModelType">vehicle model type object</param>
        /// <returns>vehicle model types</returns>
        public List<VehicleModelType> UpdateVehicleModelTypeScalar(VehicleModelType vehicleModelType)
        {
            List<VehicleModelType> vehicleModelTypes = null;
            try
            {
                int identity = base.DataProvider.InsertVehicleModelTypeScalar(vehicleModelType.VehicleModelTypeName, vehicleModelType.VehicleModelID, vehicleModelType.IsActive);
                //Insert an vehicle model
                AutoFixDataSet dataSet = base.DataProvider.GetVehicleModelTypeDetails(identity, true);
                if (dataSet != null && dataSet.VehicleModelType != null && dataSet.VehicleModelType.Rows != null)
                {
                    vehicleModelTypes = FillVehicleModelTypeObjectsByEntities(dataSet.VehicleModelType);
                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            //returns vehicleModels
            return vehicleModelTypes;

        }
        /// <summary>
        /// Gets all the vehicle model types
        /// </summary>
        /// <returns>vehicle model type list</returns>
        public List<VehicleModelType> GetVehicleModelTypes()
        {
            //The vehicle model type details
            List<VehicleModelType> vehicleModelTypeDetails = null;
            try
            {
                //Get the vehicle model details list
                vehicleModelTypeDetails = this.GetVehicleModelTypes(0, false);
            }
            catch (Exception eX)
            {
                //log error here
            }
            //The vehicle model type details list to return
            return vehicleModelTypeDetails;
        }

        /// <summary>
        /// Gets  the vehicle model types by id
        /// </summary>
        /// <returns>vehicle model type list</returns>
        public List<VehicleModelType> GetVehicleModelTypeByID(int vehicleModelTypeID)
        {
            //The vehicle model type details
            List<VehicleModelType> vehicleModelTypeDetails = null;
            try
            {
                //Get the vehicle model details list
                vehicleModelTypeDetails = this.GetVehicleModelTypes(vehicleModelTypeID, false);
            }
            catch (Exception eX)
            {
                //log error here
            }
            //The vehicle model type details list to return
            return vehicleModelTypeDetails;
        }
        #endregion

        #region [Private Methods]
        /// <summary>
        /// The method that gets the list of vehicle model types or just one vehicle model type
        /// </summary>
        /// <param name="vehicleModelTypeId">vehicle model type id</param>
        /// <param name="selectById">The check to see if select by id or not</param>
        /// <returns>vehicle model type list</returns>
        private List<VehicleModelType> GetVehicleModelTypes(int vehicleModelTypeId, bool selectById)
        {
            //The vehicle model type details to be returned
            List<VehicleModelType> vehicleModelTypeDetails = null;
            try
            {
                //Get the data set returned from the data provider
                AutoFixDataSet dataSet = base.DataProvider.GetVehicleModelTypeDetails(vehicleModelTypeId, selectById);

                if (dataSet != null && dataSet.VehicleModelType != null && dataSet.VehicleModelType.Rows != null)
                {
                    //Set the returned filled list
                    vehicleModelTypeDetails = FillVehicleModelTypeObjectsByEntities(dataSet.VehicleModelType);
                }
            }
            catch (Exception eX)
            {
                //log the error here, dont throw the exception
            }
            //Return the vehicle model type details list
            return vehicleModelTypeDetails;

        }
        /// <summary>
        /// <summary>
        /// Fill the vehicle model type object
        /// </summary>
        /// <param name="dataTable">data table</param>
        /// <returns>the vehicle model type list</returns>
        private List<VehicleModelType> FillVehicleModelTypeObjectsByEntities(AutoFixDataSet.VehicleModelTypeDataTable dataTable)
        {
            //The vehicle model type list that contains the objects
            //Loop through the data table and create new VehicleModelTypeObject 
            //finally select the VehicleModelTypeObject and convert to list
            List<VehicleModelType> vehicleModelTypeDetails = (from vmto in
                                                          (from vmt in dataTable.AsEnumerable<AutoFixDataSet.VehicleModelTypeRow>()
                                                           select new
                                                           {
                                                               VehicleModelTypeObject = new VehicleModelType()
                                                               {
                                                                   VehicleModelTypeID = vmt.VehicleModelTypeID,
                                                                   VehicleModelTypeName = vmt.VehicleModelTypeName,
                                                                   VehicleModelID = vmt.VehicleModelID,
                                                                   IsActive = vmt.IsActive
                                                               }
                                                           })
                                                      select vmto.VehicleModelTypeObject).ToList();
            //Return the filled vehicle model type details list
            return vehicleModelTypeDetails;
        }
        #endregion
    }
}
